/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.action.query;

import com.inspur.edp.bff.api.dataprocessor.IDataConvertor;
import com.inspur.edp.bff.spi.AbstractDataConvertor;
import com.inspur.edp.bff.spi.AbstractFSAction;
import com.inspur.edp.bff.spi.builtin.BffEntityResInfoImpl;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.condition.SortCondition;
import com.inspur.edp.cef.entity.entity.IEntityData;

import java.util.ArrayList;
import java.util.List;

public class QueryChildAction extends AbstractFSAction<ArrayList<IEntityData>> {
    private List<String> nodeCodes;
    private EntityFilter filter;

    public QueryChildAction(List<String> nodeCodes, EntityFilter filter) {
        this.nodeCodes = nodeCodes;
        this.filter = filter;
    }

    @Override
    public void execute() {
        transFilter();

        List<IEntityData> queryResult = getLcp().query(this.nodeCodes.get(nodeCodes.size() - 1), this.filter);
        if (queryResult == null) {
            setResult(null);
        } else {
            ArrayList<IEntityData> vmDatas = new ArrayList<>();
            for (IEntityData entityData : queryResult) {
                IEntityData vmData = getContext().createData(this.nodeCodes.get(nodeCodes.size() - 1));
                getDataConvertor().convertFromBEData(entityData, vmData);
                vmDatas.add(vmData);
            }
            setResult(vmDatas);
        }
    }

    private void transFilter() {
        if (filter == null)
            filter = new EntityFilter();

        ArrayList<SortCondition> originalSort = filter.getSortConditions();

        BffEntityResInfoImpl childEntityResInfo = (BffEntityResInfoImpl) getContext().getModelResourceInfo().getCustomResource(nodeCodes.get(nodeCodes.size() - 1));
        if (childEntityResInfo != null && childEntityResInfo.getSorts() != null && childEntityResInfo.getSorts().size() > 0) {
            filter.setSortConditions(new ArrayList<SortCondition>());
            for (SortCondition sort : childEntityResInfo.getSorts()) {
                filter.getSortConditions().add(sort);
            }
            if (originalSort != null && originalSort.size() > 0) {
                filter.addSortConditions(originalSort);
            }
        }

    }

    private AbstractDataConvertor getDataConvertor() {
        IDataConvertor dataConvertor = getContext().getDataConvertor();
        AbstractDataConvertor rootConvertor = (AbstractDataConvertor) ((dataConvertor instanceof AbstractDataConvertor) ? dataConvertor : null);
        if (this.nodeCodes.isEmpty()) {
            return rootConvertor;
        }
        AbstractDataConvertor currentConvertor = rootConvertor;
        for (int i = 0; i < this.nodeCodes.size(); i++) {
            currentConvertor = currentConvertor.getChildDataConvertor((String) this.nodeCodes.get(i));
        }
        return currentConvertor;
    }
}
